package com.potenza.training.dictionary.server.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.potenza.training.dictionary.dao.SubjectDifficultyDao;
import com.potenza.training.dictionary.entity.SubjectDifficultyEntity;
import com.potenza.training.dictionary.server.SubjectDifficultyService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("subjectDifficultyService")
public class SubjectDifficultyServiceImpl extends ServiceImpl<SubjectDifficultyDao, SubjectDifficultyEntity> implements SubjectDifficultyService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SubjectDifficultyEntity> page = this.page(
                new Query<SubjectDifficultyEntity>().getPage(params),
                new QueryWrapper<SubjectDifficultyEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SubjectDifficultyEntity> findAll() {
        Object subjectDifficultyEntity = redisTemplate.opsForValue().get("subjectDifficultyEntity");
        if (subjectDifficultyEntity==null){
            List<SubjectDifficultyEntity> list = this.list();
            redisTemplate.opsForValue().set("subjectDifficultyEntity", list);
            return list;
        }
        return (List<SubjectDifficultyEntity>) subjectDifficultyEntity;
    }

    @Override
    public List<SubjectDifficultyEntity> findByPid(int pid) {
        QueryWrapper<SubjectDifficultyEntity> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.lambda().eq(SubjectDifficultyEntity::getPid,pid);
        return baseMapper.selectList(objectQueryWrapper);
    }
}